<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>轮播图管理</title>
<!--layui脚本  -->
<link rel="stylesheet" type="text/css" href="../layui/css/layui.css" />
<script src="../layui/layui.js" type="text/javascript" charset="utf-8"></script>
</head>
<body>

	<!-- 表格容器 -->
	<table id="demo" lay-filter="test"></table>
	<!-- 头部工具栏开始 -->
	<script type="text/html" id="toolbarDemo1">
         <div class="layui-btn-container">
            <button class="layui-btn layui-btn-sm" lay-event="add"> <i class="layui-icon">&#xe654;</i>添加</button>
         </div>
     </script>
	<!-- 头部工具栏结束 -->
	<!-- 行内工具栏开始 -->
	<script type="text/html" id="toolbarDemo2">
          <div class="layui-btn-container">
            <button class="layui-btn layui-btn-sm layui-btn-danger" lay-event="delete" id="del"><i class="layui-icon">&#xe640;</i>删除</button>
            <button class="layui-btn layui-btn-sm" lay-event="update"><i class="layui-icon">&#xe642;</i>编辑</button>
          </div>
     </script>
	<!-- 行内工具栏结束 -->
	<!-- 添加的表单 -->
	<div id="addDiv" style="display: none; width: 1000">
		<form class="layui-form" style="width: 350px;" lay-filter="addForm">
			<div class="layui-form-item">
				<label class="layui-form-label">标题</label>
				<div class="layui-input-inline">
					<input type="text" name="title" required lay-verify="required"
						placeholder="请输入标题" autocomplete="off" class="layui-input">
				</div>
			</div>
			<div class="layui-form-item">
				<label class="layui-form-label">权重</label>
				<div class="layui-input-inline">
					<input type="text" name="weight" required lay-verify="required"
						placeholder="请输入标题" autocomplete="off" class="layui-input">
				</div>
			</div>
			<div class="layui-form-item">
				<label class="layui-form-label">跳转地址</label>
				<div class="layui-input-inline">
					<input type="text" name="link" required lay-verify="required"
						placeholder="请输入标题" autocomplete="off" class="layui-input">
				</div>
			</div>
			<div class="layui-form-item">
				<label class="layui-form-label" name="url">url</label>
				<div class="layui-input-inline">
					<!-- 上传图片按钮 -->
					<button type="button" class="layui-btn" id="test1">
						<i class="layui-icon">&#xe67c;</i>上传图片
					</button>
					<!-- 展示上传成功的图片 -->
					<img id="showImg" src="" width="50">
					<!-- 用来储存上传成功之后的图片路径 -->
					<input type="text" name="url" id="url" required
						lay-verify="required" placeholder="请输入标题" autocomplete="off"
						class="layui-input layui-disabled">
				</div>
			</div>
			<div class="layui-form-item">
				<div class="layui-input-block">
					<button class="layui-btn" lay-submit lay-filter="formDemo">立即提交</button>
					<button type="reset" class="layui-btn layui-btn-primary">重置</button>
				</div>
			</div>
		</form>
	</div>
	<!-- 添加的表单结束 -->



	<!-- 修改的表单开始 -->
	<div id="updDiv" style="display: none; width: 1000">
		<form class="layui-form" style="width: 350px;" lay-filter="updForm">
			<div class="layui-form-item">
				<label class="layui-form-label">id</label>
				<div class="layui-input-inline">
					<input type="text" name="id" required lay-verify="required"
						placeholder="请输入标题" autocomplete="off" class="layui-input layui-disabled">
				</div>
			</div>
			<div class="layui-form-item">
				<label class="layui-form-label">标题</label>
				<div class="layui-input-inline">
					<input type="text" name="title" required lay-verify="required"
						placeholder="请输入标题" autocomplete="off" class="layui-input">
				</div>
			</div>
			<div class="layui-form-item">
				<label class="layui-form-label">权重</label>
				<div class="layui-input-inline">
					<input type="text" name="weight" required lay-verify="required"
						placeholder="请输入标题" autocomplete="off" class="layui-input">
				</div>
			</div>
			<div class="layui-form-item">
				<label class="layui-form-label">跳转地址</label>
				<div class="layui-input-inline">
					<input type="text" name="link" required lay-verify="required"
						placeholder="请输入标题" autocomplete="off" class="layui-input">
				</div>
			</div>
			<div class="layui-form-item">
				<label class="layui-form-label" name="url">添加图片</label>
				<div class="layui-input-inline">
					<button type="button" class="layui-btn" id="test2">
						<i class="layui-icon">&#xe67c;</i>上传图片
					</button>
					<!-- 展示上传成功的图片 -->
					<img id="showImg2" src="" width="50">
					<!-- 回显图片地址 -->
					<input type="text" name="url" id="url2" required
						lay-verify="required" placeholder="请输入标题" autocomplete="off"
						class="layui-input layui-disabled">
				</div>
			</div>
			<div class="layui-form-item">
				<div class="layui-input-block">
					<button class="layui-btn" lay-submit lay-filter="formDemo2">立即提交</button>
					<button type="reset" class="layui-btn layui-btn-primary">重置</button>
				</div>
			</div>
		</form>
	</div>
	<!-- 修改的表单结束 -->

	<!-- 脚本开始 -->
	<script>
		layui.use([ 'table', 'form', 'layer', 'upload' ], function() {
			var table = layui.table;
			var form = layui.form;
			var layer = layui.layer;
			var upload = layui.upload;
			var $ = layui.$;

			//表格渲染开始
			table.render({
				elem : '#demo',
				height : 280,
				url : 'BannerServletAdmin?method=getList', //数据接口
				page : true, //开启分页
				id : "idTest",
				toolbar : '#toolbarDemo1',
				cols : [ [ //表头
				{
					field : 'id',
					title : 'ID',
					width : 80
				}, {
					field : 'title',
					title : '标题',
					width : 80
				}, {
					field : 'url',
					title : '图片地址',
					width : 180,
					templet : function(d) {
						return '<img src="../'+d.url+'"/>'
					}
				}, {
					field : 'link',
					title : '链接地址',
					width : 180
				}, {
					field : 'weight',
					title : '权重',
					width : 177
				}, {
					title : '操作',
					width : 177,
					toolbar : '#toolbarDemo2'
				} ] ]
			});//表格渲染结束

			//图片上传执行实例---添加
			var uploadInst = upload.render({//upload.render表示渲染
				elem : '#test1',//绑定元素
				url : 'ImgServletAdmin?method=uploadImg', //上传接口
				done : function(res) {
					//上传完毕回调
					console.log(res);
					if (res.code == 0) {
						$("#showImg").attr("src", "../images/" + res.data);//展示图片
						$("#url").val("images/" + res.data);//url的路径
					}
				},
				error : function() {
					//请求异常回调
				}
			});//图片上传执行实例结束

			//图片上传执行实例---修改
			var uploadInst = upload.render({//upload.render表示渲染
				elem : '#test2',//绑定元素
				url : 'ImgServletAdmin?method=uploadImg', //上传接口
				done : function(res) {
					//上传完毕回调
					console.log(res);
					if (res.code == 0) {
						$("#showImg2").attr("src", "../images/" + res.data);//展示图片
						$("#url2").val("images/" + res.data);//url的路径
					}
				},
				error : function() {
					//请求异常回调
				}
			});//图片修改执行实例结束

			//监听事件--头部工具栏监听开始
			table.on('toolbar(test)', function(obj) {
				if (obj.event == "add") {
					// layer.msg('添加');
					layer.open({
						type : 1,
						content : $('#addDiv')
					//添加div弹窗的id
					});
				}
			});//监听事件--头部工具栏监听结束

			//监听事件--行内工具栏监听开始
			table.on('tool(test)', function(obj) {
				var data = obj.data;//当前行内数据
				switch (obj.event) {
				case 'delete':
					layer.confirm('真的删除行么', function(index) {
						obj.del(); //删除对应行（tr）的DOM结构，并更新缓存
						layer.close(index);
						//向服务端发送删除指令
						$.post("BannerServletAdmin?method=delBanById", {
							"id" : data.id
						}, function(res) {
							layer.msg(res.msg);
						}, "json")
					});
					break;
				case 'update':
					//layer.msg('编辑');
					//弹窗
					layer.open({
						type : 1,
						content : $('#updDiv')
					});
					//数据回显
					form.val("updForm", data);
					$("#showImg2").attr("src", "../" + data.url);//展示图片
					break;
				}
			});//监听事件--行内工具栏监听结束

			//监听表单提交--添加
			form.on('submit(formDemo)', function(data) {
				//layer.msg(JSON.stringify(data.field));
				$.post("BannerServletAdmin?method=insertBanner", data.field,
						function(res) {
							console.log(res);
							if (res.code == 0) {
								layer.msg(res.msg, {
									time : 1000
								}, function() {
									layer.closeAll();//关闭所有窗口
									table.reload('idTest', {});//表格重载（根据表格id执行）
								})
							}
						}, "json")
				return false;
			});//监听表单提交结束--添加

			//监听表单提交--修改
			form.on('submit(formDemo2)', function(data) {
				//layer.msg(JSON.stringify(data.field));
				$.post("BannerServletAdmin?method=updateBan", data.field,
						function(res) {
							console.log(res);
							if (res.code == 0) {
								layer.msg(res.msg, {
									time : 1000
								}, function() {
									layer.closeAll();//关闭所有窗口
									table.reload('idTest', {});//表格重载（根据表格id执行）
								})
							}
						}, "json")
				return false;
			});//监听修改表单提交结束--修改

		});//脚本结束
	</script>

</body>
</html>